package org.seven.jrdp.commons.mybatis.generator;

import java.io.File;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.ModelType;
import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.config.SqlMapGeneratorConfiguration;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.seven.jrdp.commons.config.JdbcConfig;

public class GeneratorTools {
	private static final String path = "d:/MBG";
	private static final String schema = "dbo";
	private static final String tableName = "t_%";
	private static final String modelPackage = "org.seven.jrdp.biz.t.model";
	private static final String mapperPackage = "org.seven.jrdp.biz.t.mapper";

	public static void main(String[] args) throws Exception {
		File directory = new File(path);
		if (!directory.exists()) {
			directory.mkdir();
		}

		Configuration config = new Configuration();
		Context context = new Context(ModelType.FLAT);
		context.setId("mybatis3");
		config.addContext(context);

		PluginConfiguration mapperPlugin = new PluginConfiguration();
		mapperPlugin.setConfigurationType("org.seven.jrdp.commons.mybatis.generator.MapperPlugin");
		mapperPlugin.addProperty("mappers", "tk.mybatis.mapper.common.Mapper");
		context.addPluginConfiguration(mapperPlugin);

		JDBCConnectionConfiguration jdbc = new JDBCConnectionConfiguration();
		jdbc.setDriverClass(JdbcConfig.getDriverClassName());
		jdbc.setConnectionURL(JdbcConfig.url);
		jdbc.setUserId(JdbcConfig.username);
		jdbc.setPassword(JdbcConfig.getPassword());
		context.setJdbcConnectionConfiguration(jdbc);

		JavaModelGeneratorConfiguration model = new JavaModelGeneratorConfiguration();
		model.setTargetPackage(modelPackage);
		model.setTargetProject(path);
		context.setJavaModelGeneratorConfiguration(model);

		JavaClientGeneratorConfiguration mapper = new JavaClientGeneratorConfiguration();
		mapper.setConfigurationType("XMLMAPPER");
		mapper.setTargetPackage(mapperPackage);
		mapper.setTargetProject(path);
		context.setJavaClientGeneratorConfiguration(mapper);

		SqlMapGeneratorConfiguration xml = new SqlMapGeneratorConfiguration();
		xml.setTargetPackage(mapperPackage + "." + JdbcConfig.getDbType());
		xml.setTargetProject(path);
		context.setSqlMapGeneratorConfiguration(xml);

		TableConfiguration table = new TableConfiguration(context);
		table.setSchema(schema);
		table.setTableName(tableName);
		table.setSelectByExampleStatementEnabled(false);
		table.setCountByExampleStatementEnabled(false);
		table.setUpdateByExampleStatementEnabled(false);
		table.setDeleteByExampleStatementEnabled(false);
		table.addProperty("ignoreQualifiersAtRuntime", "true");
		context.addTableConfiguration(table);

		DefaultShellCallback callback = new DefaultShellCallback(true);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, null);
		myBatisGenerator.generate(null);
	}
}